#include <stdint.h>
#include <8052.h>

#include "misc.h"
#include "show_num.h"

#define FOSC 11059200L //晶振时钟
//#define T1MS (65536 - (FOSC / 12 / 100)) //10ms timer calculation method in 12T mode//  /100是10ms

#define dula P2_6 //段选位 用于数码的显示锁存
#define wela P2_7 //位选位 用于哪个数码的显示锁存

typedef unsigned char BYTE;
typedef unsigned int WORD;

//数码管16位显示数
__code BYTE table[] = {
	0x3f, 0x06, 0x5b, 0x4f,
	0x66, 0x6d, 0x7d, 0x07,
	0x7f, 0x6f, 0x77, 0x7c,
	0x39, 0x5e, 0x79, 0x71};

unsigned char temp0 = 0xfe; //11111110B
unsigned char temp1 = 0xfe;

int t0 = 0; //计时器0用于多少个毫秒
int ti = 0; //tabel index 数据索引
int t1 = 0; //计时器1用于多少个毫秒

// BYTE L = (BYTE)T1MS;		//1毫秒定时器的低位
// BYTE H = (BYTE)(T1MS >> 8); //1毫秒定时器的低位
BYTE L, H;

void showNum()
{
	unsigned int t1ms = (65536 - (FOSC / 12 / 100));
	L = (unsigned char)t1ms;
	t1ms = t1ms >> 8;
	H = (unsigned char)t1ms;


	TMOD = 0x11; //开启两个定时器

	wela = 1;  //位选开启
	P0 = 0x2a; //设置： 101010B
	wela = 0;  //位选锁定
	P0 = 0;	   //重置为不显示
	//初始显示
	P0 = table[ti];
	P1 = temp1;

	TL0 = L; //reload timer0 low byte
	TH0 = H; //reload timer0 high byte

	TL1 = L;
	TH1 = H;

	TR0 = 1; //timer0 start running
	ET0 = 1; //enable timer0 interrupt

	TR1 = 1; //timer1 start running
	ET1 = 1; //enable timer1 interrupt

	EA = 1; //open global interrupt switch
}

//定时器0 //用于显示数码管
void Timer0_Rountine(void) __interrupt 1
{
	TL0 = L;
	TH0 = H;
	if (++t0 == 100)
	{ //跑完1000ms  10ms X 100 = 1000ms = 1s
		t0 = 0;
		ti++;
		if (ti >= sizeof(table))
		{
			ti = 0;
		}
		dula = 1; //段选开启
		P0 = table[ti];
		dula = 0; //段选锁定
	}
}

//定时器1 //用于循环显示发光二级管
void Timer1_Rountine(void) __interrupt 3
{
	TL1 = L;
	TH1 = H;
	if (++t1 == 100)
	{
		t1 = 0;
		temp1 = crol(temp1); //移位
		P1 = temp1;
	}
}